﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;

public class M
{
    public static string GetQueryString(string queryName, UserControl userControl)
    {
        if (userControl.Request.QueryString[queryName] != null)
        {
            return userControl.Request.QueryString[queryName];
        }
        return null;
    }

    public static void BindEnumToCbo(DropDownList cbo, Type type)
    {
        cbo.Items.Clear();
        string[] names = type.GetEnumNames();
        Array values = type.GetEnumValues();
        List<ListItem> listItems = new List<ListItem>();
        for (int i = 0; i < names.Length; i++)
        {
            listItems.Add(new ListItem(names[i], Convert.ToInt32(values.GetValue(i)).ToString()));
        }
        cbo.DataSource = listItems;
        cbo.DataTextField = "Text";
        cbo.DataValueField = "Value";
        cbo.DataBind();
    }

    public static object GetUserId(UserControl userControl)
    {
        HttpCookie cookie = userControl.Request.Cookies["Account"];
        if (cookie != null)
        {
            return cookie["UserId"];
        }
        return null;
    }

    public static object GetUserId(Page page)
    {
        HttpCookie cookie = page.Request.Cookies["Account"];
        if (cookie != null)
        {
            return cookie["UserId"];
        }
        return null;
    }

    public static void Logout(UserControl userControl)
    {
        HttpCookie cookie = userControl.Request.Cookies["Account"];
        if (cookie != null)
        {
            cookie["UserId"] = null;
        }
    }

    public static void Logout(Page page)
    {
        HttpCookie cookie = page.Request.Cookies["Account"];
        if (cookie != null)
        {
            cookie["UserId"] = null;
        }
    }

    public static void SendEmail(string to, string cc, string from, string subject, string content)
    {
        MailMessage mailMessage = new MailMessage();
        mailMessage.To = to;
        mailMessage.Cc = cc;
        mailMessage.From = from;
        mailMessage.Subject = subject;
        mailMessage.BodyFormat = MailFormat.Text;
        mailMessage.Body = content;
        SmtpMail.Send(mailMessage);
    }

    public static IQueryable<object> Paging(IOrderedQueryable<object> query, string currentPageStr, int totalRow, ref Literal litPaging, string url)
    {
        if (query.Count()<=totalRow)
        {
            return query;
        }
        int count = query.Count()/totalRow;
        if (query.Count()%totalRow!=0)
        {
            count += 1;
        }
        int currentPage = 1;
        if (currentPageStr != null)
        {
            currentPage = Convert.ToInt32(currentPageStr);
        }
        if (currentPage>count)
        {
            currentPage = count;
        }
        string content = GetContentFromFile(GetProject() + "File/Template/Paging/Paging.htm");
        if (currentPage==1)
        {
            content = content.Replace("{displayspantruoc}", string.Empty);
            content = content.Replace("{displayatruoc}", "display:none");
            if (url.IndexOf("&Page=1") == -1)
            {
                url += "&Page=2";
            }
            else
            {
                content = content.Replace("{linksau}", url.Replace("&Page=1", "&Page=2"));
            }
            content = content.Replace("{linksau}", url);
        }
        else
        {
            content = content.Replace("{displayspantruoc}", "display:none");
            content = content.Replace("{displayatruoc}", string.Empty);
        }
        if (currentPage==count)
        {
            content = content.Replace("{displayspansau}", string.Empty);
            content = content.Replace("{displayasau}", "display:none");
            url = url.Replace("&Page=" + currentPageStr, "&Page=" + (currentPage - 1));
            content = content.Replace("{linktruoc}", url);
        }
        else
        {
            content = content.Replace("{displayspansau}", "display:none");
            content = content.Replace("{displayasau}", string.Empty);
        }
        if (currentPage>1 && currentPage<count)
        {
            content = content.Replace("{linktruoc}", url.Replace("&Page=" + currentPage, "&Page=" + (currentPage - 1)));
            content = content.Replace("{linksau}", url.Replace("&Page=" + currentPage, "&Page=" + (currentPage + 1)));
        }
        litPaging.Text = content;
        return query.Skip((currentPage -1)*totalRow).Take(totalRow);
    }

    public static string GetContentFromFile(string filePath)
    {
        try
        {
            return File.ReadAllText(filePath);
        }catch
        {
            return string.Empty;
        }
    }

    public static string GetProject()
    {
        return @"E:\baopx\Project\BanhangOnline\";
    }
}